home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <string.h>
-
- #include <A4Stuff.h>
-
-
- #include "6502.h"
- #include "global.h"
-
- #include "interpret.h"
- #include "debug.h"
-
- UInt8 GetOpCodeIndex(UInt8 theByte);
- UInt8 GetOpCode(UInt8 theByte);
- void PreCacheOpCodes(void);
- SInt8 PeekRelative(Ptr myProg, SInt8 howfar);
- UInt8 PeekABS(Ptr myProg, SInt8 howfar);
-
- UInt8 gCPUCache[sizeof(UInt8) << 3];
- static Boolean gInited = false;
- typedef pascal void (*MixedModeCallBack)(MixedMode6502DataPtr);
-
-
- pascal void main(UInt8* myProg : __A0, MixedModeCallBack theCallBack :__A1);
-
-
- pascal void main(UInt8* myProg : __A0, MixedModeCallBack theCallBack : __A1)
- {
- #pragma unused ( theCallBack)
-
- EnterCodeResource();
-
- gDone = false;
- if (! gInited) {
- InitInterpreter();
- }
-
- #define FORMATSTRING "// PC $%4.4X SP $%2.2X A $%2.2X X $%2.2X Y $%2.2X SR %8.8b"
-
- for (gStartofProg = myProg, pc = 0; !gDone;) {
- switch (gOpCodeBase[gCPUCache[*(myProg + pc)] - 1].mode) {
- // case Accumulator:
- case None:
- Debug("%-s "FORMATSTRING,
- gOpCodeBase[gCPUCache[*(myProg + pc)] - 1].name,
- (UInt16)pc, (UInt16)sp, (UInt16)regA, (UInt16)regX, (UInt16)regY, *(myProg + pc), regP);
- break;
-
- case Relative:
- Debug("%s $%.4X "FORMATSTRING,
- gOpCodeBase[gCPUCache[*(myProg + pc)] - 1].name,
- (SInt16)(((SInt16)(pc+2)) + PeekRelative((Ptr)myProg, 1)),
- (UInt16)pc, (UInt16)sp, (UInt16)regA, (UInt16)regX, (UInt16)regY, *(myProg + pc), regP);
- break;
-
- case IndX:
- Debug("%s ($%.2X,X) "FORMATSTRING,
- gOpCodeBase[gCPUCache[*(myProg + pc)] - 1].name,
- (SInt16)(PeekABS((Ptr)myProg, 1)),
- (UInt16)pc, (UInt16)sp, (UInt16)regA, (UInt16)regX, (UInt16)regY, *(myProg + pc), regP);
- break;
-
- case ZeroPage:
- Debug("%s $%.2X "FORMATSTRING,
- gOpCodeBase[gCPUCache[*(myProg + pc)] - 1].name,
- (SInt16)(PeekABS((Ptr)myProg, 1)),
- (UInt16)pc, (UInt16)sp, (UInt16)regA, (UInt16)regX, (UInt16)regY, *(myProg + pc), regP);
- break;
-
- case Immediate:
- Debug("%s #$%.2X "FORMATSTRING,
- gOpCodeBase[gCPUCache[*(myProg + pc)] - 1].name,
- (SInt16)(PeekABS((Ptr)myProg, 1)),
- (UInt16)pc, (UInt16)sp, (UInt16)regA, (UInt16)regX, (UInt16)regY, *(myProg + pc), regP);
- break;
-
- case Absolute:
- Debug("%s $%.2X%.2X "FORMATSTRING,
- gOpCodeBase[gCPUCache[*(myProg + pc)] - 1].name,
- (SInt16)(PeekABS((Ptr)myProg, 2)),
- (SInt16)(PeekABS((Ptr)myProg, 1)),
- (UInt16)pc, (UInt16)sp, (UInt16)regA, (UInt16)regX, (UInt16)regY, *(myProg + pc), regP);
- break;
-
- case IndY:
- Debug("%s ($%.2X),Y "FORMATSTRING,
- gOpCodeBase[gCPUCache[*(myProg + pc)] - 1].name,
- (SInt16)(PeekABS((Ptr)myProg, 1)),
- (UInt16)pc, (UInt16)sp, (UInt16)regA, (UInt16)regX, (UInt16)regY, *(myProg + pc), regP);
- break;
-
- case ZeroPageX:
- Debug("%s $%.2X,X "FORMATSTRING,
- gOpCodeBase[gCPUCache[*(myProg + pc)] - 1].name,
- (SInt16)(PeekABS((Ptr)myProg, 1)),
- (UInt16)pc, (UInt16)sp, (UInt16)regA, (UInt16)regX, (UInt16)regY, *(myProg + pc), regP);
- break;
-
- case AbsoluteY:
- Debug("%s $%.2X,Y "FORMATSTRING,
- gOpCodeBase[gCPUCache[*(myProg + pc)] - 1].name,
- (SInt16)(PeekABS((Ptr)myProg, 1)),
- (UInt16)pc, (UInt16)sp, (UInt16)regA, (UInt16)regX, (UInt16)regY, *(myProg + pc), regP);
- break;
-
- case AbsoluteX:
- Debug("%s $%.2X%.2X,X "FORMATSTRING,
- gOpCodeBase[gCPUCache[*(myProg + pc)] - 1].name,
- (SInt16)(PeekABS((Ptr)myProg, 2)),
- (SInt16)(PeekABS((Ptr)myProg, 1)),
- (UInt16)pc, (UInt16)sp, (UInt16)regA, (UInt16)regX, (UInt16)regY, *(myProg + pc), regP);
- break;
-
- case ZeroPageY:
- Debug("%s $%.2X,Y "FORMATSTRING,
- gOpCodeBase[gCPUCache[*(myProg + pc)] - 1].name,
- (SInt16)(PeekABS((Ptr)myProg, 1)),
- (UInt16)pc, (UInt16)sp, (UInt16)regA, (UInt16)regX, (UInt16)regY, *(myProg + pc), regP);
- break;
-
- case Indirect:
- Debug("%s ($%.2X%.2X) "FORMATSTRING,
- gOpCodeBase[gCPUCache[*(myProg + pc)] - 1].name,
- (SInt16)(PeekABS((Ptr)myProg, 2)),
- (SInt16)(PeekABS((Ptr)myProg, 1)),
- (UInt16)pc, (UInt16)sp, (UInt16)regA, (UInt16)regX, (UInt16)regY, *(myProg + pc));
- break;
-
- default:
- Debug("What the hell???");
- break;
- }
-
-
- (*(gOpCodeBase[gCPUCache[*(myProg + pc++)] - 1].fun))();
- }
-
- ExitCodeResource();
- }
-
- SInt8 PeekRelative(Ptr myProg, SInt8 howfar)
- {
- return *(myProg + (( (SInt16)pc)+howfar));
- }
-
- UInt8 PeekABS(Ptr myProg, SInt8 howfar)
- {
- return *(myProg + ((pc)+howfar));
- }
-
- void InitInterpreter(void)
- {
- regA = 0x00;
- regX = 0x00;
- regY = 0x00;
- regP = 0x00;
-
- pc = 0;
- if (gMyMemoryPages == nil)
- gMyMemoryPages = (PagePtr)NewPtrSys(sizeof(Page) * kNumMemPages);
- if (gMyMemoryPages == nil) {
- Debugger();
- }
- gPageZero = (UInt8*)gMyMemoryPages;
- gStack = &gMyMemoryPages[1].mem[0];
-
- if (!gInited)
- PreCacheOpCodes();
-
- gInited = true;
- }
-
- void PreCacheOpCodes(void)
- {
- UInt8 index = 0, opIndex;
-
- for (index = 0; index != 0xFF; index++) {
- opIndex = GetOpCodeIndex(index);
- if (opIndex < gNumOpCodes) {
- gCPUCache[index] = opIndex + 1;
- }
- else {
- gCPUCache[index] = 0;
- }
- }
- }
-
- UInt8 GetOpCode(UInt8 theByte)
- {
- UInt8 index = GetOpCodeIndex(theByte);
- if (index < gNumOpCodes)
- return gOpCodeBase[index].opCode;
-
- // Crash!
- gDone = true;
- return NOP;
- }
-
- UInt8 GetOpCodeIndex(UInt8 theByte)
- {
- UInt8 count;
- UInt8 tempOpCode;
-
- for (count = 0 ; count < gNumOpCodes; count++) {
- tempOpCode = (UInt8) (gOpCodeBase[count].opCode);
- if (theByte == tempOpCode) {
- return count;
- }
- }
- return gNumOpCodes;
- }
-
-
-